﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WssDemo.Data;
using WssDemo.Models;
using WssDemo.ViewModel;

namespace WssDemo.Controllers
{
    public class HomeController : Controller
    {
        public readonly static WssDb db = WssDemo.MvcApplication.Db;

        public ActionResult Index()
        {

            if (Session["UserName"] == null)
            {
                return RedirectToRoute(new { controller = "User", action = "Login" });
            }


            var ef = from m in db.Messages
                     orderby m.Id descending
                     select m;
            

            var mes = ef.ToList();

            var efComm = from c in db.Comments
                         join m in db.Messages on c.MessageId equals m.Id
                         join u in db.Users on m.FromUserId equals u.Id
                         join u2 in db.Users on c.ToUserId equals u2.Id
                         orderby c.Id descending
                         select new { c.MessageId, c.Comment, FromUserName = u.UserName, ToUserName = u2.UserName };

            List<SSModel> s =new List<SSModel>();

            foreach (var ss in mes)
            {
                List<CommentModel> commList =new List<CommentModel>();

                foreach (var com in efComm)
                {
                    if (com.MessageId == ss.Id)
                    {
                        commList.Add(new CommentModel
                        {
                            Comment = com.Comment,
                            ToUserName = com.ToUserName,
                            FromUserName = com.FromUserName
                        });
                    }
                    else
                    {
                        commList.Add(null);
                    }
                }

                s.Add(new SSModel
                {
                    Id = ss.Id,
                    Content = ss.Content,
                    CommentModels = commList
                });
            }



            return View(new IndexModel 
            {
                UserName = (string)Session["UserName"],
                SSModels=s
            });
        }

        [HttpPost]
        public void ClearAuth()
        {
            Session.Clear();
        }

        [HttpPost]
        public ActionResult SendSaySay(string content)
        {

            var obj = db.Messages.Add(new Messages
            {
                FromUserId = (int)Session["Id"],
                Content = content
            });


            if (Save() > 0)
            {
                return PartialView("SaySay", new SSModel
                {
                    Id = obj.Id,
                    Content = obj.Content
                });
            }
            else
            {
                return Json(new
                {
                    err = 320,
                    msg = "发表失败"
                });
            }


        }

        [HttpPost]
        public ActionResult SendComment(string content, int ssId)
        {
            var ef = from u in db.Messages
                     where u.Id == ssId
                     select u;

            var message = ef.FirstOrDefault();


            var obj = db.Comments.Add(new Comments
            {
                MessageId = message.Id,
                ToUserId = (int)Session["Id"],
                Comment = content
            });


            if (Save() > 0)
            {
                var efUser = from u in db.Users
                             where u.Id == message.FromUserId
                             select u;

                var users = efUser.FirstOrDefault();

                return PartialView("Comment", new CommentModel
                {
                    FromUserName = (string)Session["UserName"],
                    ToUserName = users.UserName,
                    Comment = content

                });
            }
            else
            {
                return Json(new
                {
                    err = 300,
                    msg = "评论失败"
                });
            }
        }

        private static int Save()
        {
            return db.SaveChanges();
        }
    }
}